package guns
{
	import enemies.ArmoredMovingTurret;
	import flash.display.Sprite;
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import ex.Circle;
	import MovingCircle;
	/**
	 * ...
	 * @author ...
	 */
	public class Bullet extends MovingCircle
	{
		static function PlayerBullet(rot:Number):Bullet
		{
			var bt:Bullet = new Bullet(rot);
			return bt;
		}
		static function EnemyBullet(rot:Number):Bullet
		{
			var bt:Bullet = new Bullet(rot, 5);
			bt.MaskCollide = MovingCircle.GroupWall;
			bt.MaskInteract = MovingCircle.GroupPlayer | MovingCircle.GroupWall | GroupJunk;
			bt.MaskPublic = 0;
			return bt;
		}
		public function Bullet(rot:Number, spread:Number=0) 
		{
			super(2.0, 12.0);
			graphics.beginFill(0xFFFFFF);
			graphics.drawEllipse(0, 0, r*2,r);
			graphics.endFill();
			rotation = rot+spread*(Math.random()-0.5)*2;
			addEventListener(Event.ENTER_FRAME, moveBullet);
			MaskCollide = MovingCircle.GroupWall;
			MaskInteract = MovingCircle.GroupEnemy | MovingCircle.GroupWall | GroupJunk;
			MaskPublic = 0;
		}
		
		override public function Interact(o:MovingCircle):Boolean
		{
			if (super.Interact(o))
			{
				if (o as ArmoredMovingTurret)
				{
					//trace ("Ricochet!");
					//rotation += 180;
					var L:Point = new Point(this.dx, this.dy);
					var N:Point = new Point(this.x - o.x, this.y - o.y);
					//trace("N=", N);
					//trace("L=", L);
					
					L = Circle.reflect(N, L);
					//trace("L2=", L);
					dx = L.x;
					dy = L.y;
					rotation=Math.atan2(dy , dx)*180 / Math.PI;
				    MaskCollide = MovingCircle.GroupPlayer | MovingCircle.GroupEnemy | MovingCircle.GroupWall;
					MaskInteract = MovingCircle.GroupPlayer | MovingCircle.GroupEnemy;
					MaskPublic = 0;
					return true;
				}
			//	trace("Hit!");
				o.Damage(15);
				MaskInteract = 0;//Never more
				this.removeMe = true;
				return true;
			}
			return false;
		}
		
		
		public function moveBullet(event:Event):void
		{
			moveForward();
		}
	}

}